interface ICarRecommend extends ng.IScope {
    
    // 开始地址
    startAddress: string;
    // 结束地址
    endAddress: string;
    // 起始地城市code
    startCityCode: string;
    // 目的地城市code
    endCityCode: string;    // 车辆列表

    // 调车推荐获取的车辆列表
    carList: carItem[];

    // 当前页
    currentPage:number;
    // 总页数
    totalPage:number;
    
    // 展现调车推荐的logo
    showLogo:boolean;
    // 展现调车推荐的表格
    showTab:boolean;
    // 加载中页面
    showLoadding:boolean;
    // 加载失败
    showFail:boolean;
    
    // 收起表格
    hideTable:()=>void;
    // 加载表格
    loadTable:()=>void;
    // 刷新
    refresh:()=>void;
    // 跳转到第一页 前一页 后一页 最后一页
    toFirst:()=>void;
    toPrev:()=>void;
    toNext:()=>void;
    toLast:()=>void;
    handleIpt:()=>void;
    // 选中以展示详情的车辆
    selectedCar:carRecommendItem;
    // 展示车辆详情
    getDetail:(car:carRecommendItem)=>void;
    // 获取车辆联系电话
    getPhone:(car:carRecommendItem)=>void;
    // 加载中text
    loaddingText:string;

}


interface carItem {
    id: string;
    type: string;
    length: string;
    weight: string;
    distance: string;
    phone: string
}


function tmsCarRecommend(carRecommendService:ICarRecommendService): ng.IDirective {
    return {
        restrict: 'E',
        templateUrl: './src/directives/tms-carRecommend/tms-carRecommend.html',

        transclude: true,
        scope: {
            startAddress: "=",
            endAddress: "=",
            startCityCode: "=",
            endCityCode: "="
        },
        link: ($scope: ICarRecommend) => {

            init();

            $scope.carList = []
            // state
            $scope.currentPage = 1;
            $scope.totalPage = 5;
            // private varite
            
            var privateTimeOut;
            var latitude;
            var longitude;
            var carList;

            var index=0;

            // $scope.$watch('startAddress', (newVal, oldVal) => {
            //     console.log(newVal);
            // }, true);

            function init() {
                $scope.selectedCar=null;
                showStage('showLogo');
                carRecommendService.getPositionFromAddress($scope.startAddress).then(res=>{
                    console.log(res);
                    latitude=res.data.lat;
                    longitude=res.data.lon;
                });
                $scope.loaddingText="加载中";
                setInterval(()=>{
                    index++;
                    $scope.loaddingText="加载中"+".......".slice(0,index%7);
                    if (!$scope.$$phase) {
                        $scope.$apply(() => { $scope.loaddingText });
                    }
                },500)

               
            }

            $scope.loadTable = () => {
                // $scope. = false;
                showStage('showLoadding');
                if(!$scope.carList.length){
                    carRecommendService.getCarRecommendList(longitude,latitude,$scope.startCityCode,$scope.endCityCode).then(res=>{
                        showStage('showTab');
                        carList=res.data.data;
                        $scope.carList=carList.slice(0,10);
                        $scope.totalPage=Math.ceil(carList.length/10)
                    },rej=>{
                        showStage('showFail')
                    })
                }else{
                    showStage('showTab')
                }
                

            }
            $scope.hideTable = () => {
                // $scope.carList=[];
                showStage('showLogo')
            }
            $scope.refresh = () => {
                showStage('showLoadding');
                
                carRecommendService.getCarRecommendList(longitude,latitude,$scope.startCityCode,$scope.endCityCode).then(res=>{
                    showStage('showTab');
                    carList=res.data.data;
                    $scope.currentPage=1;
                    $scope.carList=carList.slice(0,10);
                    $scope.totalPage=Math.ceil(carList.length/10);
                },rej=>{
                    showStage('showFail')
                })
            }
            
            $scope.toFirst = () => { 
                 loadPage(1)  
            }
            $scope.toPrev = () => { 
                loadPage($scope.currentPage-1)
            }
            $scope.toNext = () => { 
                loadPage($scope.currentPage+1)
            }
            $scope.toLast = () => {
                loadPage($scope.totalPage)
            }
            $scope.handleIpt=()=>{
                    if(!$scope.currentPage){
                        return
                    }
                    
                    clearTimeout(privateTimeOut);
                    privateTimeOut = setTimeout(()=>{
                        console.log($scope.currentPage);
                        let tempPage=Number($scope.currentPage)
                        if(isNaN(tempPage)){
                            $scope.currentPage=1;
                        }else if(1>tempPage){
                            $scope.currentPage=1;
                        }else if(tempPage>$scope.totalPage){
                            $scope.currentPage=$scope.totalPage;
                        }
                        if (!$scope.$$phase) {
                            $scope.$apply(() => { $scope.currentPage });
                        }
                    },300)
                
                
            }

            $scope.getDetail=(car:carRecommendItem)=>{
                
                $scope.selectedCar=car;
                if(!car.address){
                    carRecommendService.getAddressFromPosition(car.lat,car.lon).then(res=>{
                        console.log(res.data)
                        car.address=res.data.address;
                    })
                }

                
            }
            $scope.getPhone=(car:carRecommendItem)=>{
                console.log(car.carId);
                if(!car.phone){
                    carRecommendService.getCarOwenerInfo(car.carId).then(res=>{
                        car.carCode=res.data.carCode;
                        car.phone=res.data.phone;
                        car.name=res.data.name;
                    })
                }

            }

            

            function loadPage(page:number){
                if(1>page||page>$scope.totalPage){
                    return;
                }else{
                    $scope.currentPage=page;
                    var startIndex=(page-1)*10;
                    var endIndex=page*10>carList.length?carList.length:page*10;
                    $scope.carList=carList.slice(startIndex,endIndex);
                }
            }

            function showStage(stage){
                ['showLogo','showTab','showLoadding','showFail'].forEach(item=>{
                    if(item===stage){
                        $scope[item]=true;
                    }else{
                        $scope[item]=false;
                    }
                })
                
            }

            







        }

    }
}


tmsCarRecommend['$inject'] = ['carRecommendService']
angular.module('tmsCarRecommend', ['carRecommendService']).directive('tmsCarRecommend', tmsCarRecommend);